package com.hooper.hosp.controller.api;

import com.hooper.hosp.model.hosp.Department;
import com.hooper.hosp.model.hosp.Hospital;
import com.hooper.hosp.model.hosp.Schedule;
import com.hooper.hosp.service.DepartmentService;
import com.hooper.hosp.service.HospService;
import com.hooper.hosp.service.ScheduleService;
import com.hooper.hosp.vo.hosp.DepartmentQueryVo;
import com.hooper.hosp.vo.hosp.ScheduleQueryVo;
import com.hooper.hospital.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.hooper.hospital.common.result.ResultCodeEnum.OBJECT_IS_NULL;
import static com.hooper.hospital.common.result.ResultCodeEnum.SIGN_ERROR;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/09/28/10:33
 */
@Api("hosp api")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/hosp")
public class ApiController {

    private final HospService hospService;

    private final DepartmentService departmentService;

    private final ScheduleService scheduleService;

    @ApiOperation("save a hosp")
    @PostMapping("saveHospital")
    public Result<String> saveHosp(HttpServletRequest request) {
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        String logoData = (String) map.get("logoData");
        map.put("logoData", logoData.replaceAll(" ", "+"));
        hospService.saveOrUpdate(map);
        return Result.ok();
    }

    @ApiOperation("get hosp by its hoscode")
    @PostMapping("hospital/show")
    public Result<Hospital> showHosp(HttpServletRequest request) {
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        String hoscode = (String) map.get("hoscode");
        Hospital hospital = hospService.getHospitalByHoscode(hoscode);
        return hospital == null ? Result.fail(OBJECT_IS_NULL) : Result.ok(hospital);
    }

    @ApiOperation("save department by json ")
    @PostMapping("saveDepartment")
    public Result<String> save(HttpServletRequest request) {
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        return departmentService.saveOrUpdate(map) ? Result.ok() : Result.fail();
    }

    @ApiOperation("get department list")
    @PostMapping("department/list")
    public Result<Page<Department>> getDepartmentList(HttpServletRequest request) {
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        boolean hasPage = StringUtils.isNotEmpty((String) map.get("page"));
        int page = hasPage ? Integer.parseInt((String) map.get("page")) : 1;
        boolean hasLimit = StringUtils.isNotEmpty((String) map.get("limit"));
        int limit = hasLimit ? Integer.parseInt((String) map.get("limit")) : 1;
        DepartmentQueryVo departmentVo = new DepartmentQueryVo();
        departmentVo.setHoscode((String) map.get("hoscode"));
        return Result.ok(departmentService.pageDepartment(page, limit, departmentVo));
    }

    @ApiOperation("remove one department")
    @PostMapping("department/remove")
    public Result<String> remove(HttpServletRequest request) {
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");

        departmentService.removeByHoscode(hoscode, depcode);
        return Result.ok();
    }

    @ApiOperation("save the schedules by json")
    @PostMapping("saveSchedule")
    public Result<String> saveSchedule(HttpServletRequest request) {
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        scheduleService.saveOrUpdate(map);
        return Result.ok();
    }

    @ApiOperation("get all schedule list")
    @PostMapping("schedule/list")
    public Result<Page<Schedule>> pageSchedule(HttpServletRequest request) {
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        boolean hasPage = StringUtils.isNotEmpty((String) map.get("page"));
        int page = hasPage ? Integer.parseInt((String) map.get("page")) : 1;
        boolean hasLimit = StringUtils.isNotEmpty((String) map.get("limit"));
        int limit = hasLimit ? Integer.parseInt((String) map.get("limit")) : 1;

        ScheduleQueryVo queryVo = new ScheduleQueryVo();
        queryVo.setHoscode((String) map.get("hoscode"));

        Page<Schedule> list = scheduleService.pageSchedule(page, limit, queryVo);
        return Result.ok(list);
    }


    @ApiOperation("remove one schedule by id")
    @PostMapping("schedule/remove")
    public Result<String> removeOne(HttpServletRequest request){
        Map<String, Object> map = hospService.signKeyCheckAndMapSwitch(request);
        scheduleService.removeByHoscodeAndScheduleId((String)map.get("hoscode"),(String)map.get("scheduleId"));
        return Result.ok();
    }
}
